package cn.jtfadmin.activiti.controller.rest;

import cn.jtfadmin.activiti.domain.dto.FormDTO;
import cn.jtfadmin.activiti.domain.dto.ModelAddPDTO;
import cn.jtfadmin.activiti.domain.dto.ModelDTO;
import cn.jtfadmin.activiti.service.ActFormService;
import cn.jtfadmin.base.lang.common.domain.dto.PageRDTO;
import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import cn.jtfadmin.base.lang.common.utils.RequestUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.FormService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ModelQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程-表单
 * @author jtf
 * @since 0.0.2
 */
@RestController
@Validated
public class ActivitiFormController implements ModelDataJsonConstants {



    @Autowired
    private ActFormService actFormService;


    /**
     * 获取启动表单
     * @param deploymentId 部署id
     * @return
     */
    @GetMapping(value = "/activiti/form/getStartFormInfo")
    public ResultDTO<FormDTO> getStartFormInfo(@RequestParam @NotBlank String deploymentId){
        FormDTO formData = actFormService.getStartFormInfoByDeploymentId(deploymentId);
        return ResultDTO.successData(formData);
    }
    /**
     * 获取任务表单
     * @param taskId 任务id
     * @return
     */
    @GetMapping(value = "/activiti/form/getTaskFormInfo")
    public ResultDTO<FormDTO> getTaskFormInfo(@RequestParam @NotBlank String taskId){
        FormDTO formData = actFormService.getTaskFormInfoByTaskId(taskId);
        return ResultDTO.successData(formData);
    }


    /**
     * 启动流程， 输入流程中是部署表单信息
     * @param deploymentId 部署id
     * @return
     */
    @PostMapping(value = "/activiti/form/startProcess/{deploymentId}")
    public ResultDTO<String> startProcess(@PathVariable String deploymentId){
        String formData = null;
        try(ServletInputStream inputStream = RequestUtils.getRequest().getInputStream()){
             formData = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        actFormService.startForm(deploymentId, formData);
        return ResultDTO.success();
    }

    /**
     * 任务表单完成， 输入流程中是任务表单信息
     * @deprecated 因为记录中不能记录任务id
     * @see ActivitiTaskController#taskFormComplate(String)
     * @param taskId 任务idid
     * @return
     */
    @Deprecated
    @PostMapping(value = "/activiti/form/taskFormSubmit/{taskId}")
    public ResultDTO<String> taskFormSubmit(@PathVariable String taskId){
        String formData = null;
        try(ServletInputStream inputStream = RequestUtils.getRequest().getInputStream()){
            formData = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        actFormService.taskFormSubmit(taskId, formData);
        return ResultDTO.success();
    }

}

